﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel;
using System.Data;
using System.Diagnostics;

namespace XNADota.GamePlay
{
    public class SLKFile
    {
        private DataSet _DataSet;

        public SLKFile(string filePath)
        {
            using (FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

                excelReader.IsFirstRowAsColumnNames = true;

                _DataSet = excelReader.AsDataSet();

                excelReader.Close();
            }
        }

        public void Merge(string filePath)
        {
            using (FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

                excelReader.IsFirstRowAsColumnNames = true;
                DataSet result = excelReader.AsDataSet();

                _DataSet.Merge(result);
                _DataSet.AcceptChanges();

                excelReader.Close();
            }
        }

        public void CreateObjects(EObjectType type, ObjectTypeDictionary typeDictionary, bool isMetaData, ObjectModifyFile modifyFile)
        {
            if (isMetaData)
            {
                typeDictionary.MetaDataSet = this._DataSet;

                //if (type == EObjectType.ItemData)
                //{
                //    DataTable table = typeDictionary.MetaDataSet.Tables[0];
                //    DataRow[] rows = typeDictionary.MetaDataSet.Tables[0].Select("useItem = 1");
                //    table.Rows.Clear();
                //    for (int i = 0; i < rows.Length; i++)
                //        table.Rows.Add(rows[i]);
                //    table.AcceptChanges();
                //}
            }
            else
            {
                Debug.Assert(modifyFile != null);

                // Create Original Table
                typeDictionary.OriginalDataSet = _DataSet.Copy();

                // Change the original files
                ObjectModifyTable originModifyTable = modifyFile.OriginalTable;
                for (int modifyUnitIndex = 0; modifyUnitIndex < originModifyTable.Count; modifyUnitIndex++)
                {
                    ObjectModifyUnit modifyUnit = originModifyTable.ObjectModifyUnits[modifyUnitIndex];

                    string key = modifyUnit.OriginalObjectId;

                    for (int modifyIndex = 0; modifyIndex < modifyUnit.ModifiedCount; modifyIndex++)
                    {
                        ObjectModifyProperty modifyProperty = modifyUnit.ObjectModifyProperties[modifyIndex];

                        typeDictionary.ChangeField(typeDictionary.OriginalDataSet, key, modifyProperty);
                    }
                }

                // Create Custom Table
                typeDictionary.CustomDataSet = _DataSet.Copy();
                typeDictionary.ClearRows(typeDictionary.CustomDataSet);

                ObjectModifyTable customModifyTable = modifyFile.ChangedTable;
                for (int modifyUnitIndex = 0; modifyUnitIndex < customModifyTable.Count; modifyUnitIndex++)
                {
                    ObjectModifyUnit modifyUnit = customModifyTable.ObjectModifyUnits[modifyUnitIndex];

                    string key = modifyUnit.OriginalObjectId;
                    string newKey = modifyUnit.ChangedObjectId;

                    typeDictionary.CopyRow(this._DataSet, typeDictionary.CustomDataSet, key, newKey);

                    for (int modifyIndex = 0; modifyIndex < modifyUnit.ModifiedCount; modifyIndex++)
                    {
                        ObjectModifyProperty modifyProperty = modifyUnit.ObjectModifyProperties[modifyIndex];

                        typeDictionary.ChangeField(typeDictionary.CustomDataSet, newKey, modifyProperty);
                    }
                }
            }
        }

        //private static int fileNo = 0;
        private void PrintDebugInfo()
        {
            //using (FileStream file = new FileStream("debug" + fileNo++ + ".txt", FileMode.Create, FileAccess.Write))
            //{
            //    StreamWriter writer = new StreamWriter(file);

            //    for (int i = 0; i < _DataTable.Rows.Count; i++)
            //    {
            //        for (int j = 0; j < _DataTable.Columns.Count; j++)
            //            writer.Write(string.Format("{0}####",_DataTable.Rows[i][_DataTable.Columns[j]].ToString()));
            //        writer.WriteLine("");
            //    }

            //    writer.Close();
            //    file.Close();
            //}
        }

        //public void ApplyChanges(ObjectModifyFile modifyFile, SLKFile metaTable)
        //{
        //    // Change the original files
        //    ObjectModifyTable originModifyTable = modifyFile.OriginalTable;
        //    for (int modifyUnitIndex = 0; modifyUnitIndex < originModifyTable.Count; modifyUnitIndex++)
        //    {
        //        ObjectModifyUnit modifyUnit = originModifyTable.ObjectModifyUnits[modifyUnitIndex];

        //        for (int modifyIndex = 0; modifyIndex < modifyUnit.ModifiedCount; modifyIndex++)
        //        {
        //            ObjectModifyProperty modifyProperty = modifyUnit.ObjectModifyProperties[modifyIndex];
        //        }
        //    }
        //}

    }
}
